﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Scavenger
{
    class Animation
    {
        /* Class description
         * The animation class is initialized with:
         *  - A sprite strip (containing the different frames for the animation)
         *  - Position
         *  - Individual frame width and height
         *  - The number of frames
         *  - The time each frame is displayed (in milliseconds)
         *  - Color
         *  - Scale
         *  - If the frame is to be looped (otherwise deactivated when animation reaches the last frame)
         *  
         * When updated, the class will determine if it should change frame, depending on
         * the time elapsed since last frame change using the gameTime parameter.
         *
         */

        #region Delcarations

        public Vector2 Position;

        public int FrameWidth, FrameHeight;
        public bool Active, Looping;

        Rectangle sourceRect = new Rectangle();
        Rectangle destinationRect = new Rectangle();
        Texture2D spriteStrip;
        Color color;

        float scale;
        int elapsedTime, frameTime, frameCount, currentFrame, scaledFrameWidth, scaledFrameHeight;


        #endregion

        public void Initialize(Texture2D texture, Vector2 position,
                                int frameWidth, int frameHeight, int frameCount,
                                int frametime, Color color, float scale, bool looping)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.Looping = looping;
            this.Position = position;
            this.spriteStrip = texture;
            this.scale = scale;

            if (scale != 1f)
            {
                scaledFrameWidth = (int)(this.FrameWidth * this.scale);
                scaledFrameHeight = (int)(this.FrameHeight * this.scale);
            }
            else
            {
                scaledFrameWidth = this.FrameWidth;
                scaledFrameHeight = this.FrameHeight;
            }
                       
            elapsedTime = 0;
            currentFrame = 0;

            Active = true;
        }

        public void Update(GameTime gameTime)
        {
            if (!Active)
                return;

            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (elapsedTime >= frameTime)
            {
                currentFrame++;

                if (currentFrame == frameCount)
                {
                    currentFrame = 0;

                    if (!Looping)
                        Active = false;
                    
                }
                elapsedTime = 0;
            }

            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
            destinationRect = new Rectangle((int)Position.X - scaledFrameWidth / 2,
                                (int)Position.Y - scaledFrameHeight / 2,
                                scaledFrameWidth, scaledFrameHeight);
    
        }

        public void Draw(SpriteBatch spriteBatch, float rotation)
        {
            if (Active)
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, Color.White, rotation, 
                    new Vector2(FrameWidth / 2, FrameHeight / 2), SpriteEffects.None, 0f);
            
        }
    }
}
